<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <style type="text/css">
      body{background:#f5f5f5;}
      .con{
        width:100%; text-align:center; padding:25px;
      }
      .btn{
        width:200px; height:50px; line-height:50px; background:#e2e2e2; margin:0px auto 20px auto;
      }
      .txt{height:50px; line-height:50px; font-size:14px; color:#333;}
    </style>
</head>
<body>
  <div id="App" class="hide">
    <div class="con">
      <div class="txt" v-show="is_download==1">已下载数据：{{cacheSize}}M</div>
      <div class="btn" tapmode="" onclick="vue.startDownload()">
        {{is_download==1?'重新下载数据':'开始下载数据'}}
      </div>
    </div>
  </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/vue.min.js"></script>
<script type="text/javascript">
    apiready = function(){

        vue = new Vue({
            el: '#App',
            data: {
              did:0,
              destination:{},
              destinations:[],
              albums:[],
              dealedAlbums:[],
              plays:[],
              files:[],

              TaskK:0, //文件下载 K
              Task:[], //文件下载队列

              cacheSize:0,
              is_download:0,
            },
            methods: {
                init:function(){

                },
                pushInTask: function(fileUrl){
                  var that = this;
                  if(!fileUrl || fileUrl == '' || fileUrl == undefined){
                    return '';
                  }
                  for(var i in that.Task){
                    if(that.Task[i]==fileUrl){
                      return i;
                    }
                  }
                  var k = that.TaskK;
                  that.Task[k] = fileUrl;
                  that.TaskK += 1;
                  return k;
                },
                startDownload: function(){
                  var that = this;
                  if(that.is_download==1){
                    api.confirm({
                        title: '温馨提示',
                        msg: '重新下载会清除以前的数据，是否继续？',
                        buttons: ['确定', '取消']
                    }, function(ret, err) {
                        if(ret && ret.buttonIndex==1){
                          that.clearDownlaoded(function(){
                            that.startingDownload();
                          });
                        }
                    });
                  }else {
                    that.startingDownload();
                  }
                },
                clearDownlaoded: function(cb){
                  var that = this;
                  api.clearCache(function() {
                      that.is_download = 0;
                      cb&&cb();
                  });
                },
                startingDownload: function(){
                  var that = this;
                  api.showProgress({title: '数据下载中', text: '博物馆数据...'});
                  ajax.get('destination/name/', {did:that.did}, function(res){
                    if (res.code!=0) {
                      api.alert({title: '温馨提示', msg: res.msg});
                      return false;
                    }
                    var destination = res.data.child;
                    $api.setStorage('destination', destination);
                    that.downloadDestinations();
                  });
                },
                downloadDestinations:function(){
                  var that = this;
                  api.showProgress({title: '数据下载中', text: '博物馆列表...'});
                  ajax.get('destination/find/', {pid:that.did, p:1}, function(res){
                    if (res.code!=0) {
                      api.alert({title: '温馨提示', msg: res.msg});
                      return false;
                    }
                    that.destinations = res.data.rows;
                    $api.setStorage('destinations', that.destinations);
                    that.downloadAlbums(0);
                  });
                },
                downloadAlbums: function(k){
                  var that = this;
                  var destinations = that.destinations;
                  var len = destinations.length;
                  api.showProgress({title: '数据下载中', text: '专辑数据...'});
                  if(k<len){
                    ajax.get('Album/find/', {did:destinations[k].did}, function(res){
                      if (res.code!=0) {
                        api.alert({title: '温馨提示', msg: res.msg});
                        return false;
                      }
                      var albums = res.data.albums;
                      for(var i in albums){
                        albums[i].head = that.pushInTask(albums[i].head);
                        var audio = albums[i].audio;
                        albums[i].audio = audio?res.data.resource+'32/'+audio:'';
                        albums[i].audio = that.pushInTask(albums[i].audio);
                      }
                      that.albums[destinations[k].did] = albums;
                      that.downloadAlbums(k+1);
                    });
                  }else{
                    $api.setStorage('albums', that.albums);
                    that.dealAlbums(0);
                  }
                },
                dealAlbums:function(k){
                  var that = this;
                  var destinations = that.destinations;
                  var len = destinations.length;
                  var albums = that.albums;

                  api.showProgress({title: '数据下载中', text: '处理专辑数据...'});
                  if(k<len){
                    var album = albums[destinations[k].did];
                    for(var i in album){
                      album[i].did = destinations[k].did;
                      that.dealedAlbums.push(album[i]);
                    }
                    that.dealAlbums(k+1);
                  }else{
                    that.downloadPlays(0);
                  }
                },
                downloadPlays: function(k){
                  var that = this;
                  var albums = that.dealedAlbums;
                  var len = albums.length;

                  api.showProgress({title: '数据下载中', text: '播放数据...'});
                  if(k<len){
                    var did = albums[k].did;
                    var aid = albums[k].album_id;
                    ajax.get('Album/speech/', {did:did, album_id:aid}, function(res){

                      if (res.code!=0) {
                        api.alert({title: '温馨提示', msg: res.msg});
                        return false;
                      }

                      var _d = res.data.rows;
                      for(var i in _d){
                          _d[i].map = that.pushInTask(_d[i].map);
                          for(var j in _d[i].point){
                            _d[i].point[j].description = '';
                            _d[i].point[j].images = that.pushInTask(_d[i].point[j].images);
                            if(_d[i].point[j].speech){
                              var audio = _d[i].point[j].speech.audio;
                              _d[i].point[j].speech.audio = audio?res.data.resource+'32/'+audio:'';
                              _d[i].point[j].speech.audio = that.pushInTask(_d[i].point[j].speech.audio);
                            }
                            for(var kk in _d[i].point[j].extend){
                              var audio =   _d[i].point[j].extend[kk].audio;
                              _d[i].point[j].extend[kk].audio = audio?res.data.resource+'32/'+audio:'';
                              _d[i].point[j].extend[kk].audio = that.pushInTask(_d[i].point[j].extend[kk].audio);
                              _d[i].point[j].extend[kk].avatar = that.pushInTask(_d[i].point[j].extend[kk].avatar);
                            }
                          }
                        }

                        var data = {
                          rows:_d,
                          album:res.data.album,
                          destination:res.data.destination
                        };

                      that.plays[aid] = data;
                      that.downloadPlays(k+1);
                    });
                  }else{
                    $api.setStorage('plays', that.plays);
                    that.downloadfiles(0);
                  }
                },
                downloadfiles: function(k){
                  var that = this;
                  var len = that.Task.length;

                  if(k<len){
                    api.showProgress({title: '数据下载中', text: k+'/'+len});
                    var fileUrl = that.Task[k];
                    if(-1==fileUrl.indexOf('.mp3')){
                      fileUrl = fileUrl + '-middle';
                    }
                    ajax.download(fileUrl, '', function(res){
                      if (res.state==1) {
                          that.files[k] = res.savePath;
                      }else{
                        that.files[k] = '';
                      }
                      that.downloadfiles(k+1);
                    });
                  }else{
                    api.hideProgress();
                    $api.setStorage('files', that.files);
                    $api.setStorage('download', '1');
                    api.alert({title: '温馨提示', msg: '数据下载完毕'}, function(){
                      api.sendEvent({
                          name: 'reloadEvent',
                          extra: {}
                      });
                      api.closeToWin({name: 'root'});
                    });
                  }
                }
            },
            created: function() {
              var that = this;
              var user = $api.getStorage('user');
              that.did = user.did;

              var is_download = $api.getStorage("download");
              var is_download = (!is_download || is_download==undefined || is_download==null || is_download=="")?0:is_download;
              that.is_download = is_download;

              api.getCacheSize(function(ret) {
                  that.cacheSize = (ret.size/1024/1024).toFixed(2);
              });
              $api.removeCls($api.byId('App'), 'hide');
            }
        });

    };
</script>
</html>
